home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 045a / btp15.zip / CRUNCH3.PAS < prev    next >
Pascal/Delphi Source File  |  1991-11-08  |  6KB  |  178 lines

  1. PROGRAM Crunch3;              { (c) 1991 John C. Leon   last updated 11/4/91 }
  2.  
  3. {Handles ONLY standard, fixed length Btrieve files.  Uses Step Next Extended
  4.  to retrieve 5 records at a time, then Insert Extended to insert 5 records at
  5.  a time.}
  6.  
  7. {$IFDEF production} {$D-,R-,L-,S-} {$ENDIF}
  8.  
  9. USES
  10.    BTP;
  11.  
  12. CONST
  13.    NumRecordsinOp  :integer = 5; {MaxInsBufLength = (2+(2*Number of Insertions) + }
  14.    MaxInsBufLength = 20462;      { (MaxFixedRecLength*Number of Insertions)       }
  15.  
  16. TYPE
  17.    POrgFile      = ^TOrgFile;
  18.    TOrgFile      = object(BFileExt)
  19.                       function BTExt(OpCode, Key: integer): integer; virtual;
  20.                       end;
  21.  
  22.    TExtDBuffer = record
  23.                     Count: word;
  24.                     Repeater: array[1..MaxInsBufLength-2] of byte;
  25.                     end;
  26.    PCopyFile     = ^TCopyFile;
  27.    TCopyFile     = object(BFixed)
  28.                       ExtDBuffer: TExtDBuffer;
  29.                       function BTExt(OpCode, Key: integer): integer;
  30.                       end;
  31.  
  32. VAR
  33.    OrgName, CopyName     : string[79];
  34.    OrgFile               : POrgFile;
  35.    CopyFile              : PCopyFile;
  36.    Counter,  Counter1,
  37.    Counter2, CopyOfs,
  38.    OrgOfs, RecordLength,
  39.    Remainder, NumberOps  : integer;
  40.    LoRecordLength,
  41.    HiRecordLength        : byte;
  42.  
  43.  
  44. function TOrgFile.BTExt(OpCode, Key: integer): integer;
  45. begin
  46.    {call ancestor method to set buffer lengths & to structure send buffer}
  47.    BStatus := BFileExt.BTExt(OpCode, Key);
  48.    BTExt   := Btrv(OpCode, PosBlk, ExtDBuffer^.Entire, DBufferLen,
  49.                    VarNotRequired, Key);
  50. end;
  51.  
  52. function TCopyFile.BTExt(OpCode, Key: integer): integer;
  53. var
  54.    ExtBufLen: integer;
  55. begin
  56.    ExtBufLen := 2 + (2 * ExtDBuffer.Count) + (Specs.RecLen * ExtDBuffer.Count);
  57.    BTExt := Btrv(OpCode, PosBlk, ExtDBuffer, ExtBufLen, KBuffer, Key);
  58. end;
  59.  
  60.  
  61. (* Begin MAIN program code *)
  62. (* ------------------------------------------------------------------------ *)
  63. BEGIN
  64.  
  65. write('Name of file to copy from: ');
  66. readln(OrgName);
  67. for Counter := 1 to length(OrgName) do
  68.    OrgName[Counter] := upcase(OrgName[Counter]);
  69.  
  70. write('Name of file to create and populate from file ''', OrgName,''': ');
  71. readln(CopyName);
  72. for Counter := 1 to length(CopyName) do
  73.    CopyName[Counter] := upcase(CopyName[Counter]);
  74.  
  75. { Open original file in read only mode }
  76. OrgFile  := new(POrgFile, Init(OrgName, ReadOnly));
  77.  
  78. if BStatus <> Zero then
  79.  
  80.    writeln('Error opening ', OrgName)
  81.  
  82.    else
  83.  
  84.    begin                     {if original file exists and no error on open op}
  85.  
  86.    if OrgFile^.NumRecs = 0 then                  {don't proceed if empty file}
  87.       begin
  88.       writeln('No records in ', OrgName, '.  CRUNCH aborted.');
  89.       halt;
  90.       end;
  91.  
  92.    if (OrgFile^.Specs.FileFlags and 1) = 1 then    {don't do var length files}
  93.       begin
  94.       writeln(OrgName, ' is a variable length file.  Can''t process.');
  95.       halt;
  96.       end;
  97.  
  98.    RecordLength   := OrgFile^.Specs.RecLen;
  99.    LoRecordLength := lo(RecordLength);
  100.    HiRecordLength := hi(RecordLength);
  101.  
  102.    {Set up required filter and extractor data fields in OrgFile^.}
  103.    with OrgFile^ do
  104.       begin
  105.       Filter.MaxSkip       := 1;
  106.       Filter.NumLogicTerms := 0;
  107.       Extractor.NumRecords := 5;
  108.       Extractor.NumFields  := 1;
  109.       end;
  110.  
  111.    {Set up required minimum of one extractor spec in collection.  Note that
  112.     OrgFile's constructor initialized the collection.}
  113.    with OrgFile^.ExtractorSpec^ do
  114.       insert(new(PExtSpec, Init(OrgFile^.Specs.RecLen, 0)));
  115.  
  116.    {It's SOOoo easy to clone a file on the fly with this unit!}
  117.    BStatus := CloneFile(OrgName, CopyName);
  118.    if BStatus = Zero then
  119.       writeln(CopyName, ' created successfully.')
  120.       else
  121.       begin
  122.       writeln('Error creating ', CopyName, '.  Status = ', BStatus, '.');
  123.       halt;
  124.       end;
  125.  
  126.    {Open new copy of file in accelerated mode.}
  127.    CopyFile := new(PCopyFile, Init(CopyName, Accel));
  128.  
  129.    OrgOfs   := 7;  {we know the length of record in this case, and don't
  130.                     care about position, so skip the six lead bytes of each
  131.                     record}
  132.    CopyOfs  := 1;
  133.  
  134.    Remainder := OrgFile^.NumRecs MOD NumRecordsinOp;
  135.    NumberOps := OrgFile^.NumRecs DIV NumRecordsinOp;
  136.    if Remainder <> 0 then NumberOps := NumberOps + 1;
  137.  
  138.    Counter2  := Zero;
  139.  
  140.    for Counter := 1 to NumberOps do
  141.       begin
  142.       BStatus := OrgFile^.BTExt(BStepNextExt, Zero);
  143.       NumRecordsinOp := OrgFile^.ExtDBuffer^.NumRecs; {# recs ret'd by StepNextExt}
  144.  
  145.       {Build buffer for insertion.}
  146.       for Counter1 := 1 to NumRecordsinOp do
  147.          begin
  148.          CopyFile^.ExtDBuffer.Repeater[CopyOfs] := LoRecordLength;
  149.          inc(CopyOfs);
  150.          CopyFile^.ExtDBuffer.Repeater[CopyOfs] := HiRecordLength;
  151.          inc(CopyOfs);
  152.          move(OrgFile^.ExtDBuffer^.Repeater[OrgOfs],
  153.               CopyFile^.ExtDBuffer.Repeater[CopyOfs], RecordLength);
  154.          CopyOfs := CopyOfs + RecordLength;
  155.          OrgOfs  := OrgOfs + RecordLength + 6;
  156.          end;
  157.  
  158.       CopyFile^.ExtDBuffer.Count := NumRecordsinOp;
  159.       BStatus := CopyFile^.BTExt(BInsertExt, Zero);
  160.  
  161.       CopyOfs := 1;
  162.       OrgOfs  := 7;
  163.       Counter2 := Counter2 + NumRecordsinOp;
  164.       writeln('Inserted total of ', Counter2, ' records');
  165.  
  166.       end; {for Counter := 1 to NumberOps}
  167.  
  168.    writeln('DONE...');
  169.  
  170.    BStatus := OrgFile^.Close;
  171.    BStatus := CopyFile^.Close;
  172.  
  173.    dispose(OrgFile, Done);
  174.    dispose(CopyFile, Done);
  175.    end; {if BStatus <> Zero}
  176.  
  177. END.
  178.